package guodaowang.apidata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class GetAPIData {
    public static void main(String[] args) throws Exception {
        long startUtc = 1579708860;
        int counts =1;
        String response = getData(startUtc,0);
        boolean result = strtojson(response,counts);
        while (result){
            String datas = getData(startUtc,counts * 1000);
            counts++;
            result = strtojson(datas, counts);
        }
    }
    public static String getData(long startUtc,int startRow) throws Exception {
        //请求地址
        String path = "http://219.143.235.34/crawler/publishdata/blockinfo.do?startRow="+startRow+"&account=hmrc_guodaowang&type=BLOCK&password=3C1FB6C719502BAF827A50C77A9352E977A4839C&startUtc="+startUtc;
        URL url = new URL(path);
        //获取链接
        URLConnection conn = url.openConnection();
        //获取返回值
        InputStream inputStream = conn.getInputStream();
        //转换成String类型
        String response = IOUtils.toString(inputStream);
        return response;

    }

    public static boolean strtojson(String str,int counts) throws Exception {
        int total = 0;
        if (StringUtils.isNotEmpty(str)){
            JSONObject jsonObject = JSON.parseObject(str);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            //链接数据库属性
            String url = "jdbc:mysql://rm-2ze3575842g7av270ro.mysql.rds.aliyuncs.com:3306/guodaowang?useSSL=true";
            String username = "user01";
            String passwrod = "1q2w3e!@#";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn  = DriverManager.getConnection(url, username, passwrod);
            int nums=0;
            for(int i =0;i<jsonArray.size();i++){
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String sql = "insert into apidata values(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                PreparedStatement statement = conn.prepareStatement(sql);
                if(jsonObject1.get("end_stake")==null){
                    statement.setString(1,null);
                }else {
                    statement.setString(1, jsonObject1.get("end_stake").toString());
                }
                if(jsonObject1.get("bridge_or_tunnel_id")==null){
                    statement.setString(2,null);
                }else {
                    statement.setString(2, jsonObject1.get("bridge_or_tunnel_id").toString());
                }
                if(jsonObject1.get("block_type")==null){
                    statement.setString(3,null);
                }else {
                    statement.setString(3, jsonObject1.get("block_type").toString());
                }
                if(jsonObject1.get("control_start_stake")==null){
                    statement.setString(4,null);
                }else {
                    statement.setString(4, jsonObject1.get("control_start_stake").toString());
                }
                if(jsonObject1.get("open_desc")==null){
                    statement.setString(5,null);
                }else {
                    statement.setString(5,jsonObject1.get("open_desc").toString());
                }
                if(jsonObject1.get("block_reason_id")==null){
                    statement.setString(6,null);
                }else {
                    statement.setString(6, jsonObject1.get("block_reason_id").toString());
                }
                if(jsonObject1.get("block_reason_id")==null){
                    statement.setString(7,null);
                }else {
                    statement.setString(7,jsonObject1.get("stake_points").toString());
                }
                if(jsonObject1.get("isdeleted")==null){
                    statement.setString(8,null);
                }else {
                    statement.setString(8, jsonObject1.get("isdeleted").toString());
                }
                if(jsonObject1.get("update_time")==null){
                    statement.setString(9,null);
                }else {
                    statement.setString(9, jsonObject1.get("update_time").toString());
                }
                if(jsonObject1.get("estimate_time")==null){
                    statement.setString(10,null);
                }else {
                    statement.setString(10, jsonObject1.get("estimate_time").toString());
                }
                if(jsonObject1.get("resume_time")==null){
                    statement.setString(11,null);
                }else {
                    statement.setString(11, jsonObject1.get("resume_time").toString());
                }
                if(jsonObject1.get("scene_desc")==null){
                    statement.setString(12,null);
                }else {
                    statement.setString(12, jsonObject1.get("scene_desc").toString());
                }
                if(jsonObject1.get("report_user_id")==null){
                    statement.setString(13,null);
                }else {
                    statement.setString(13, jsonObject1.get("report_user_id").toString());
                }
                if(jsonObject1.get("control_end_stake")==null){
                    statement.setString(14,null);
                }else {
                    statement.setString(14,jsonObject1.get("control_end_stake").toString());
                }
                if(jsonObject1.get("measures_name")==null){
                    statement.setString(15,null);
                }else {
                    statement.setString(15, jsonObject1.get("measures_name").toString());
                }
                if(jsonObject1.get("deal_method")==null){
                    statement.setString(16,null);
                }else {
                    statement.setString(16, jsonObject1.get("deal_method").toString());
                }
                if(jsonObject1.get("block_reason")==null){
                    statement.setString(17,null);
                }else {
                    statement.setString(17, jsonObject1.get("block_reason").toString());
                }
                if(jsonObject1.get("block_location")==null){
                    statement.setString(18,null);
                }else {
                    statement.setString(18, jsonObject1.get("block_location").toString());
                }
                if(jsonObject1.get("dist_id_list")==null){
                    statement.setString(19,null);
                }else {
                    statement.setString(19, jsonObject1.get("dist_id_list").toString());
                }
                if(jsonObject1.get("road_id")==null){
                    statement.setString(20,null);
                }else {
                    statement.setString(20, jsonObject1.get("road_id").toString());
                }
                if(jsonObject1.get("start_stake")==null){
                    statement.setString(21,null);
                }else {
                    statement.setString(21, jsonObject1.get("start_stake").toString());
                }
                if(jsonObject1.get("block_way_type")==null){
                    statement.setString(22,null);
                }else {
                    statement.setString(22, jsonObject1.get("block_way_type").toString());
                }
                if(jsonObject1.get("block_id")==null){
                    statement.setString(23,null);
                }else {
                    statement.setString(23, jsonObject1.get("block_id").toString());
                }
                if(jsonObject1.get("unit_name")==null){
                    statement.setString(24,null);
                }else {
                    statement.setString(24, jsonObject1.get("unit_name").toString());
                }
                if(jsonObject1.get("block_mileage")==null){
                    statement.setString(25,null);
                }else {
                    statement.setString(25, jsonObject1.get("block_mileage").toString());
                }
                if(jsonObject1.get("rep_user_phone")==null){
                    statement.setString(26,null);
                }else {
                    statement.setString(26,jsonObject1.get("rep_user_phone").toString());
                }
                if(jsonObject1.get("province_id")==null){
                    statement.setString(27,null);
                }else {
                    statement.setString(27, jsonObject1.get("province_id").toString());
                }
                if(jsonObject1.get("block_way")==null){
                    statement.setString(28,null);
                }else {
                    statement.setString(28, jsonObject1.get("block_way").toString());
                }
                if(jsonObject1.get("detect_time")==null){
                    statement.setString(29,null);
                }else {
                    statement.setString(29, jsonObject1.get("detect_time").toString());
                }
                if(jsonObject1.get("is_impblock")==null){
                    statement.setString(30,null);
                }else {
                    statement.setString(30, jsonObject1.get("is_impblock").toString());
                }
                statement.executeUpdate();
                nums++;
            }
            JSONObject summary = jsonObject.getJSONObject("summary");
            total = Integer.parseInt(summary.get("total").toString());
            System.out.println("请求数据总条数:"+total+"条");
            System.out.println("第"+counts+"次保存数据："+nums+"条");
            System.out.println("完成!");
            System.out.println("startUtc:"+summary.get("startUtc").toString());
            System.out.println("endUtc:"+summary.get("endUtc").toString());
            if(conn!=null){
                conn.close();
            }
        }
        if(total>counts*1000){
            return true;
        }
        return false;

    }
}
